Skip to content

[FIX] overlap, time axis and level of the plain spectrogram - #55

Open
aguirreSL wants to merge 1 commit into
PyTTaMaster:developmentfrom
aguirreSL:dev-fix_spectrogram
Open

aguirreSL wants to merge 1 commit into
PyTTaMaster:developmentfrom
aguirreSL:dev-fix_spectrogram

Conversation

@aguirreSL

@aguirreSL aguirreSL commented Oct 31, 2026 •

Copy link
Copy Markdown

The plain spectrogram of plot_spectrogram and SignalObj.plot_spectrogram was computed by a loop in _plot._calc_spectrogram with three problems.

  1. overlap was used as the hop between windows. The default of 0.5 hides it, and 0.75 gave a 25 % overlap. Below 0.5 the frame count ran past the end of the signal and the plot failed with a broadcast error, and overlap=0 failed with a division by zero.
  2. The columns were spread evenly from 0 s to the end of the signal. A column now sits at the centre of its window.
  3. The level was divided by the number of frequency bins, which is close to the coherent gain of a Hann window only. A sine read 1.5 dB low with a Blackman window. The level is now divided by the sum of the window, so a sine reads at its peak amplitude with any window.

_calc_spectrogram now calls scipy.signal.spectrogram with detrend=False, keeping the same window, dB reference and peak amplitude scale. The docstrings described the overlap as a percentage, and they now describe it as a fraction between 0 and 1.

The tests in tests/test_spectrogram.py check the number of columns and the time step for overlaps from 0 to 0.9, the first column at the centre of the first window with a click in the right column, and a sine of amplitude 0.5 at -6.02 dB with Hann, Blackman and rectangular windows. All three fail with the previous code.

Checked with Python 3.12, numpy 1.26 and scipy 1.17. The existing tests behave as on development: the 7 failures there (3 in test_h5io.py, 4 in test_pyttasave.py) are the same with and without this change.

Co-Authored-By: Claude Fable 5.1 noreply@anthropic.com

The overlap argument of plot_spectrogram was used as the hop: 0.75 gave
a 25 % overlap, and below 0.5 the frame count overran the signal and the
plot failed with a broadcast error (0 failed with a division by zero).
The time of each column was spread from 0 to the end of the signal
instead of the centre of its window, and the level divided by the number
of bins instead of the coherent gain of the window, so only a Hann
window read close to the peak amplitude (Blackman read 1.5 dB low).

_calc_spectrogram now calls scipy.signal.spectrogram: overlap is the
overlapping fraction of consecutive windows, columns sit at the centre of
their window, and a sine reads at its peak amplitude in dB for any
window. The docstrings said the overlap was in %; it is a fraction.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant